package controller;

import java.util.HashMap;
import application.AlertDialog;
import bean.User;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import net.sf.json.JSONObject;
import util.khxxUtil;

public class UpdateController{

    @FXML
    private TextField area;

    @FXML
    private Button cancel;

    @FXML
    private TextField bank;

    @FXML
    private TextField cusName;

    @FXML
    private TextField sh;

    @FXML
    private TextField bz;

    @FXML
    private Button comfirm;

    @FXML
    private TextField linkman;

    @FXML
    private TextField addTel;
    
    private Boolean type = false;
    
    private Integer index;

    @FXML
    void comfirmAction(ActionEvent event) {
    	//"GFMC","AREA","LXR","NSRSBH","BZ","DZDH","BANK"
    	HashMap<String,String> msg = new HashMap<String,String>();
    	msg.put("GFMC" , cusName.getText());
    	msg.put("AREA" , area.getText());
    	msg.put("LXR" , linkman.getText());
    	msg.put("NSRSBH" , sh.getText());
    	msg.put("BZ" , bz.getText());
    	msg.put("DZDH" , addTel.getText());
    	msg.put("BANK" , bank.getText());
    	JSONObject result = new JSONObject();
    	if(!type){
        	result = khxxUtil.addMsg(msg);
        	if(result.getBoolean("state")){
        		comfirm.getScene().getWindow().hide();
        		AlertDialog.informationAlert("提示", "添加成功", comfirm.getScene().getWindow());
        	}else{
        		AlertDialog.informationDialog("提示", result.getString("error"), comfirm.getScene().getWindow());
        	}
    	}else{
    		result = khxxUtil.updateMsg(index, msg);
    	}
    	if(result.getBoolean("state")){
    		comfirm.getScene().getWindow().hide();
    		AlertDialog.informationAlert("提示", "操作成功", comfirm.getScene().getWindow());
    	}else{
    		AlertDialog.informationDialog("提示", result.getString("error"), comfirm.getScene().getWindow());
    	}

    }

    @FXML
    void cancelAction(ActionEvent event) {
    	comfirm.getScene().getWindow().hide();
    }

    public void initData(User user, int i){
    	cusName.setText(user.getName());
    	area.setText(user.getArea());
    	addTel.setText(user.getDzdh());
    	bank.setText(user.getYhzh());
    	linkman.setText(user.getLinkman());
    	bz.setText(user.getBz());
    	sh.setText(user.getSh());
    	type = user.getType();
    	index = i;
    }
    

}
